package com.zhang.admin.controller;

import com.zhang.admin.service.UserService;
import com.zhang.admin.vo.CreateUpdateUserParams;
import com.zhang.admin.vo.DeleteUserParams;
import com.zhang.admin.vo.QueryUserByPageParams;
import com.zhang.common.core.constant.MsgTemplate;
import com.zhang.common.core.page.Navigator;
import com.zhang.common.core.po.admin.SysUser;
import com.zhang.common.core.vo.Result;
import com.zhang.common.core.dto.UserInfo;
import com.zhang.admin.vo.ViewUserVo;
import com.zhang.common.log.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import static com.zhang.common.core.vo.Result.success;

@RequestMapping("/user")
@RestController
public class Usercontroller {
    @Autowired
    private UserService userService;


    /**
     * 权限认证，此接口
     * @param username
     * @return
     */
    @GetMapping("/info/{username}")
    public Result<UserInfo> findByUsername(@PathVariable(value = "username") String username) {
        UserInfo userInfo = userService.findByUsername(username);

        return  Result.success(userInfo);

    }

    /**
     * 获取当前用户全部信息
     *
     * @return 用户信息
     */
    @GetMapping
    public Result<ViewUserVo> info() {
        ViewUserVo viewUserVo = userService.currentUserInfo();
        return  Result.success(viewUserVo);

    }


    @Log(message = "'创建用户 [ '+ #createUserVo.username +' ]'")
    @PostMapping
    @PreAuthorize("hasAuthority('user:create')")
    public Result<String> createUser(CreateUpdateUserParams createUserVo){
        userService.createUser(createUserVo);
      return  success(MsgTemplate.RESULT_SAVE_OK);
    }

    @Log(message = "'更新用户 [ '+ #updateUserVo.username +' ]'")
    @PutMapping
    @PreAuthorize("hasAuthority('user:update')")
    public Result<String> updateUser( UpdateUserParams updateUserVo){
        userService.updateUser(updateUserVo);
        return  success(MsgTemplate.RESULT_UPDATE_OK);
    }

    @Log(message = "'删除用户 [ '+ #deleteUserVo.username +' ]'")
    @DeleteMapping
    @PreAuthorize("hasAuthority('user:delete')")
    public Result<String> deleteUser( DeleteUserParams deleteUserVo){
        userService.deleteUser(deleteUserVo);
        return  success(MsgTemplate.RESULT_DELETE_OK);
    }


    @GetMapping("page")
    public Result<Navigator<SysUser>> queryUserByPage(QueryUserByPageParams querySysUserByPageParams){
        Navigator<SysUser> viewUserVoList = userService.querySysUserByPage(querySysUserByPageParams);
        return  Result.success(viewUserVoList);
    }



}
